package com.xc;

import com.xc.entity.User;
import com.xc.mapper.DataSource;
import com.xc.mapper.DataSourceImpl;

import java.util.Scanner;

public class Main {
    //数据库
    private static DataSource dataSource;

    public static void main(String[] args) {
        dataSource = new DataSourceImpl();
        while (true) {
            Scanner sc = new Scanner(System.in);
            System.out.println("输入0:注册");
            System.out.println("输入1:登录");
            System.out.println("输入2:退出");
            System.out.println("请输入:");

            String next = sc.nextLine().trim();
            if ("0".equals(next)) {
                System.out.println("请输入用户名:");
                String name = sc.nextLine().trim();
                if ("".equals(name)) {
                    System.out.println("用户名不能为空");
                    continue;
                }
                User user = new User();
                user.setName(name);
                if (dataSource.haved(user)) {
                    System.out.println("用户名存在,请重新输入");
                    continue;
                }
                System.out.println("请输入密码:");
                String password = sc.nextLine().trim();
                if ("".equals(password)) {
                    System.out.println("密码不能为空");
                    continue;
                }
                user.setPassword(password);
                dataSource.save(user);
                System.out.println("录入成功");
            } else if ("1".equals(next)) {
                System.out.println("请输入用户名:");
                String name = sc.nextLine().trim();
                if ("".equals(name)) {
                    System.out.println("用户名不能为空");
                    continue;
                }
                System.out.println("请输入密码:");
                String password = sc.nextLine().trim();
                if ("".equals(password)) {
                    System.out.println("密码不能为空");
                    continue;
                }
                User user = new User();
                user.setName(name);
                user.setPassword(password);
                if (dataSource.check(user)) {
                    System.out.println("登录成功");
                } else {
                    System.out.println("登录失败");
                }

            } else if ("2".equals(next)) {
                break;
            }
        }
        System.out.println("结束..");
    }
}
